﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RedemptoristRetreatCenter.DatabaseUtility;
using RedemptoristRetreatCenter.HelperUtility;

namespace RedemptoristRetreatCenter
{
    /// <summary>
    /// Class for functionality to add retreats to the database.
    /// </summary>
    public partial class AddRetreat : System.Web.UI.Page
    {
 
        protected void Page_Load(object sender, EventArgs e)
        {
        
        }
        /// <summary>
        /// Send title, description, duration, fee, and category id from the page, to the database utility 
        /// to add the new retreat to the database.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void addRetreatButton_Click(object sender, EventArgs e)
        {
            RetreatInfo retreatInfo = new RetreatInfo();
            ConversionUtility converter = new ConversionUtility();   // TODO: should be static
            retreatInfo.title = tbTitle.Text;
            retreatInfo.description = tbDesc.Text;
            retreatInfo.duration = converter.stringToDouble(tbDuration.Text);
            retreatInfo.fee = converter.stringToDouble(tbFee.Text);
            retreatInfo.categoryId = converter.stringToInt(categoryList.Items[categoryList.SelectedIndex].Value);

            RetreatUtility retreatUtility = new RetreatUtility();
            bool success = retreatUtility.insertRetreat(retreatInfo, getUsername());

            if (success)
            {
                statusMessage.Text = "Retreat successfully added";
            }
            else
            {
                statusMessage.Text = "Unable to add retreat";
            }

        }

        private string getUsername()
        {
            if (Session["Username"] != null)
            {
                return Session["Username"].ToString();
            }
            else
            {
                return String.Empty;
            }
        }



        protected void categoryList_OnLoad(object sender, EventArgs e)
        {
            if (categoryList.Items.Count == 0) // this fires everytime control is touched.  only need to load once
            {
                RetreatUtility retreatUtility = new RetreatUtility();
                List<RetreatCategoryInfo> retreatCategories = retreatUtility.getAllRetreatCategories();
                categoryList.Items.Add(new ListItem("--Select a Category --", "0"));

                for (int cntr = 0; cntr < retreatCategories.Count; cntr++)
                {
                    // add all categories (shortDescription will be displayed and categoryId will not)
                    categoryList.Items.Add(new ListItem(retreatCategories[cntr].shortDescription, retreatCategories[cntr].categoryId.ToString()));
                }
            }
        }

    }
}